<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span style="margin-right:10px">{{artObj.title}}</span>
          <!-- 单张图片 -->
          <img class="thumb" v-if="artObj.cover.type === 1" v-lazy="artObj.cover.images[0]" :error="img404">
        </div>
        <!-- 多张图片 -->
        <div class="thumb-box" v-if="artObj.cover.type > 1">
          <img class="thumb" v-for="(imgUrl, index) in artObj.cover.images" :key="index" v-lazy="imgUrl"
               :error="img404">
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{artObj.aut_name}}</span>
            <span>{{artObj.comm_count}}</span>
            <span>{{formatTime(artObj.pubdate)}}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" v-if="iconShow" @click.stop="show = true" />
        </div>
      </template>
    </van-cell>
    <!-- 点‘x’号的弹出遮罩层 -->
    <van-action-sheet v-model="show" round :actions="actions" :cancel-text="bottomText" @select="onSelect"
                      @cancel="onCancel" @close="onClose" get-container="body" />

    <!-- 点击二级反馈选项弹出输入框 -->
    <van-dialog v-model="showDialog" show-cancel-button get-container="body" @confirm="onConfirm">
      <van-field v-model="remark" rows="5" autosize label="问题" type="textarea" maxlength="100" placeholder="请描述问题"
                 show-word-limit />

    </van-dialog>
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import { firstActions, secondActions } from '@/api/report.js'
import Notify from '@/ui/Notify.js'
export default {
  name: 'ArticleItem',
  props: {
    artObj: Object, // 文章对象
    iconShow: {
      // 外部传入，反馈（x）按钮，默认为真
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      formatTime: timeAgo, // formatTime是一个函数
      show: false,
      showDialog: false,
      actions: firstActions,
      img404:
        'https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=404%E5%9B%BE%E7%89%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=4064683576,4020230661&os=4056303190,3624235759&simid=3449517347,291727460&pn=1&rn=1&di=7117150749552803841&ln=1711&fr=&fmq=1663831463839_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%253A%252F%252Fpic4.zhimg.com%252Fv2-fc044a235e2c25da02782af0d8cb0504_1200x500.jpg%26refer%3Dhttp%253A%252F%252Fpic4.zhimg.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Dauto%3Fsec%3D1666423461%26t%3De0f103fbaf10c448ef043232fe32e48e&rpstart=0&rpnum=0&adpicid=0&nojc=undefined&dyTabStr=MCwzLDIsNCwxLDYsNSw3LDgsOQ%3D%3D',
      bottomText: '取消', // 底部按钮的文字
      remark: '', // 举报反馈
      type: 0 // 举报类型（action.value）
    }
  },
  // created() {
  //   console.log(this.artObj)
  // },
  methods: {
    // 点击遮罩层选项时触发事件
    async onSelect(action, index) {
      if (action.name === '反馈垃圾内容') {
        this.bottomText = '返回'
        this.actions = secondActions
      } else if (action.name === '不感兴趣') {
        this.$emit('dislike', this.artObj.art_id)
        this.show = false
      } else {
        // 二级反馈选项被点击，弹出输入框
        this.remark = ''
        this.type = action.value
        this.showDialog = true
      }
    },
    // 点击遮罩层返回按钮时触发事件
    onCancel() {
      if (this.bottomText === '返回') {
        this.show = true
        this.actions = firstActions
        this.bottomText = '取消'
      }
    },
    // 确定提交问题描述
    onConfirm() {
      if (this.type === 0) {
        Notify({ type: 'danger', message: '请描述问题！' })
        return
      }
      this.$emit('report', this.artObj.art_id, this.type, this.remark)
      this.show = false
    },
    // 关闭遮罩层时触发事件
    onClose() {
      setTimeout(() => {
        this.actions = firstActions
        this.bottomText = '取消'
      }, 1000)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .van-icon {
    color: rgb(209, 209, 209);
  }
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}

/* 图片样式 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 图片盒子（三张）样式 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
